package com.mobile.challenge.service.impl;

import com.mobile.challenge.core.AbstractService;
import com.mobile.challenge.dao.ChallengeInfoMapper;
import com.mobile.challenge.dao.PrizeMapper;
import com.mobile.challenge.dao.SignUpRecordMapper;
import com.mobile.challenge.dao.UserMapper;
import com.mobile.challenge.model.ChallengeInfo;
import com.mobile.challenge.model.PrizeRecord;
import com.mobile.challenge.service.ChallengeInfoService;
import com.mobile.challenge.service.PrizeRecordService;
import com.mobile.challenge.web.PrizeController;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by CodeGenerator on 2018/06/04.
 */
@Service
@Transactional
public class ChallengeInfoServiceImpl extends AbstractService<ChallengeInfo> implements ChallengeInfoService {
    @Resource
    private ChallengeInfoMapper tChallengeInfoMapper;
    @Resource
    private PrizeServiceImpl tPrizeServiceImpl;
    @Resource
    private SignUpRecordMapper tSignUpRecordMapper;
    @Resource
    private PrizeMapper tPrizeMapper;
    @Resource
    private PrizeRecordService prizeRecordService;
    @Resource
    private UserMapper tUserMapper;

    //当日剩余挑战次数
    @Override
    public Map findByPhoneNoToday(String phoneNo) {
        //今天已挑战次数
        List<ChallengeInfo> challengeList=tChallengeInfoMapper.queryCountByPhoneNoToday(phoneNo);
        //每人每天挑战上限次数
        int timesOneDay = tPrizeServiceImpl.findById(1).getTimesOneDay();
        Map rs = new HashMap();

        if(challengeList.size()==0){
            //当日未参加过挑战，有三次挑战机会
            rs.put("count",3);
        }
        else{
            int count = 0;
            //当日剩余挑战次数
            count = timesOneDay - challengeList.size();
            if(count<0){
                count = 0;
            }
            rs.put("count",count);
        }

        return rs;
    }
    //开始挑战
    @Override
    public Map start(ChallengeInfo challengeInfo) {
        Map resultData = new HashMap();
        //获取挑战次数
        List<ChallengeInfo> challengeInfoList= tChallengeInfoMapper.queryCountByPhoneNoToday(challengeInfo.getPhoneNo());
        if(challengeInfoList.size()<3){
            //记录挑战信息
            tChallengeInfoMapper.insertSelective(challengeInfo);

            //获取报名有效期
            int signUpIndate = tPrizeMapper.selectByPrimaryKey(1).getSignUpIndate();
            //报名状态置成无效
            tUserMapper.updateStatusByPhoneNoAndSingInDate(0,challengeInfo.getPhoneNo());
            resultData.put("code",true);
            resultData.put("msg","");
        }
        else{
            resultData.put("code",false);
            resultData.put("msg","今天的挑战机会已用光");
        }

        return resultData;
    }
    //结束挑战
    @Override
    public Map end(ChallengeInfo challengeInfo) {
        Map resultData = new HashMap();
        try {
        //记录挑战信息
        tChallengeInfoMapper.updateByPrimaryKeySelective(challengeInfo);
        //报名状态置成无效
        tUserMapper.updateStatusByPhoneNoAndSingInDate(0,challengeInfo.getPhoneNo());
        //获取挑战前的奖池
        int prizeTotal = tPrizeMapper.selectByPrimaryKey(1).getPrizeTotal();
        //该获奖次数是否达到上限
        int c = (int)prizeRecordService.findByPhoneNo(challengeInfo.getPhoneNo()).get("code");
        /*
            判断是否挑战成功，成功添加获奖记录
         */
        if(challengeInfo.getChallengeGrade() == 10000){

            //中奖次数未达到上限 且 奖池中还有奖
            if( c == 1 && prizeTotal >0){
                PrizeRecord prizeRecord = new PrizeRecord();
                prizeRecord.setPhoneNo(challengeInfo.getPhoneNo());
                //记录中奖信息
                prizeRecordService.add(prizeRecord);
                resultData.put("code",true);
                resultData.put("msg","恭喜获奖");
            }
            else{
                resultData.put("code",false);
                resultData.put("msg","中奖次数达到上限 或 奖池中没奖了");
            }
        }
        else{
            resultData.put("code",false);
            resultData.put("msg","成绩不满足10S");
        }
            resultData.put("count",prizeTotal);
            resultData.put("noGet",c);

        } catch (Exception e) {
            resultData.put("code",false);
            resultData.put("msg","记录挑战成绩失败");
            resultData.put("count",0);
            e.printStackTrace();
            return resultData;
        }
        return resultData;
    }

}
